//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》'
import { scrollMixin } from "utils/mixin";
import { Toast } from 'vant';
import resultItem from '../comm/resultItem.vue';
import { Image as VanImage } from 'vant';
export default {
  name:'',
  mixins: [scrollMixin], //加载更多
  //import引入的组件需要注入到对象中才能使用
  components: {VanImage,resultItem},
  data() {
  return {
    query:{
      id:'',
      keyword:'',
      page:1,
    },
    resultArr:[],
    totalPage:0,
    isLoadMore: true,

    // 海报按钮
    posterShow: false,
    posterImg: null,


    isFrontCanvas: false, //是否前端控制生成海报，false为后端生成
    host:'',
  };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    initData() {
      console.log(this);
      this.query.keyword ='';
      this.query.page =1;
      this.resultArr = [];
    },
    onSearch(val) {
      this.query.keyword = val;
      this.goodsList();
    },
    examineClick(data) {
      console.log(data);
      let {id} = data.item;
      this.postShow(id);
    },
    CloseBtn() {
      this.posterShow = false;
    },
     //海报生成
     postShow(id) {
      let url = "";
      let Jons = {};
      url = 'plugin.event-registration.api.transcript.view-certificate';

      Jons.apply_id = id;
      $http
        .get(url, Jons, "")
        .then(response => {
          if (response.result == 1) {
            let res = response.data;
            if(res.type == 2) {
              this.posterImg = res.url;
              this.posterShow = true;
            } else {
              if(res.url) {
                window.location.href = res.url;
              }
            }
            
          } else {
            this.posterShow = false;
            this.$dialog.alert({ message: response.msg });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    goodsList() {
      $http.get("plugin.event-registration.api.transcript.getList", this.query, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.isLoadMore = true;
            this.totalPage = res.last_page;
            if (!this.totalPage) {
              this.query.page = 0;
            }
            this.resultArr = res.data;
          } else {
            this.query.page = this.query.page - 1;
            this.isLoadMore = false;
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.query.page >= this.totalPage) {
        return;
      } else {
        this.query.page = this.query.page+1;
        $http.get("plugin.event-registration.api.transcript.getList", this.query, "正在获取").then(
          response => {
            if (response.result == 1) {
              let res = response.data;
              let list = res.data;
              this.isLoadMore = true;
              this.resultArr = [...this.resultArr, ...list];
              this.total = res.total;
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
              this.$dialog.alert({ message: response.msg });
            }
          },
          function(response) {
            // console.log(response);
            // error callback
          }
        );
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
  
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    this.host = JSON.parse(localStorage.getItem("yz_basic_info")).host || "";
    this.initData();
    if(this.$route.query.id) {
      this.query.id = this.$route.query.id;
    }
    this.goodsList();
  }, //如果页面有keep-alive缓存功能，这个函数会触发
  }